﻿Imports System.Drawing
Imports System.Windows.Forms

Public Class TimeZonesPage
    Friend WithEvents currentApppointment As Outlook.AppointmentItem
    Shared WithEvents cApp As Outlook.AppointmentItem
#Region "Form Region Factory"

    <Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Appointment)> _
    <Microsoft.Office.Tools.Outlook.FormRegionName("Outlook2007DIT.TimeZonesPage")> _
    Partial Public Class TimeZonesPageFactory

        ' Occurs before the form region is initialized.
        ' To prevent the form region from appearing, set e.Cancel to true.
        ' Use e.OutlookItem to get a reference to the current Outlook item.
        Private Sub TimeZonesPageFactory_FormRegionInitializing(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs) Handles Me.FormRegionInitializing
            cApp = e.OutlookItem
        End Sub

    End Class

#End Region

    'Occurs before the form region is displayed. 
    'Use Me.OutlookItem to get a reference to the current Outlook item.
    'Use Me.OutlookFormRegion to get a reference to the form region.
    Private Sub TimeZonesPage_FormRegionShowing(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.FormRegionShowing
        Me.currentApppointment = cApp
        'timeZones.Select()
        'startDate.Value = cApp.Start
        'startTime.Value = cApp.Start
        'startDate.Format = DateTimePickerFormat.Short
        'endDate.Value = cApp.End
        'endTime.Value = cApp.End

        'startDate.CustomFormat = "ddd mm-dd-yy"

        dgvTZMatrix.Enabled = True
        dgvTZMatrix.CurrentCell = Nothing
        dgvTZMatrix.RowHeadersVisible = False
        dgvTZMatrix.AllowUserToOrderColumns = False
        dgvTZMatrix.AllowUserToResizeColumns = False
        dgvTZMatrix.AllowUserToResizeRows = False
        dgvTZMatrix.AllowUserToAddRows = False
        dgvTZMatrix.AllowUserToDeleteRows = False
        'dgvTZMatrix.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(231, 239, 255)

        dgvTZMatrix.SelectionMode = Windows.Forms.DataGridViewSelectionMode.FullRowSelect
        dgvTZMatrix.DefaultCellStyle.SelectionBackColor = Drawing.Color.Transparent
        dgvTZMatrix.DefaultCellStyle.SelectionForeColor = My.Settings.selectedRowFontColor

        dgvTZMatrix.ShowCellToolTips = False
        ShowTZMatrix()

    End Sub

    'Occurs when the form region is closed.   
    'Use Me.OutlookItem to get a reference to the current Outlook item.
    'Use Me.OutlookFormRegion to get a reference to the form region.
    Private Sub TimeZonesPage_FormRegionClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.FormRegionClosed

    End Sub

    Sub ShowTZMatrix()



        AddAppointmentTimeZoneColumns()

        Dim i As Integer
        For i = 0 To dgvTZMatrix.Columns.Count - 1
            dgvTZMatrix.Columns.Item(i).SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
            dgvTZMatrix.Columns(i).Width = 60
        Next i



        Dim newRec(dgvTZMatrix.Columns.Count * 2) As String
        Dim iterateTime As DateTime
        Dim temp As DateTime
        Dim workHoursStart As DateTime
        Dim workHoursEnd As DateTime

        With cApp.Start
            iterateTime = New DateTime(.Year, .Month, .Day, 7, 0, 0)
            workHoursStart = New DateTime(.Year, .Month, .Day, 7, 0, 0)

            workHoursEnd = New DateTime(.Year, .Month, .Day, 18, 0, 0)
        End With
        'temp = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(temp, cApp.StartTimeZone.ID)

        Dim j As Integer
        Dim newrow As Integer
        Dim newRecHighlight(dgvTZMatrix.Columns.Count) As System.Drawing.Color
        Try
            For i = 1 To 24
                With dgvTZMatrix.Rows
                    For j = 0 To dgvTZMatrix.Columns.Count - 1
                        temp = iterateTime
                        If dgvTZMatrix.Columns(j).HeaderText <> "" Then
                            temp = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(temp, dgvTZMatrix.Columns(j).Name)
                            workHoursStart = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(temp, dgvTZMatrix.Columns(j).Name)
                            workHoursStart = workHoursStart.Parse(temp.ToString(System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern) & " 7:00 am")

                            workHoursEnd = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(temp, dgvTZMatrix.Columns(j).Name)
                            workHoursEnd = workHoursStart.Parse(temp.ToString(System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern) & " 6:00 pm")
                            If (temp >= workHoursStart) And (temp <= workHoursEnd) Then
                                'dgvTZMatrix.Rows(newrow).Cells(0).Style.BackColor = System.Drawing.Color.FromArgb(231, 239, 255)
                                newRecHighlight(j) = My.Settings.workHoursBackgroundColor 'System.Drawing.Color.FromArgb(231, 239, 255)
                                newRecHighlight(j + 1) = My.Settings.workHoursBackgroundColor ' System.Drawing.Color.FromArgb(231, 239, 255)
                            Else
                                newRecHighlight(j) = System.Drawing.Color.White
                                newRecHighlight(j + 1) = System.Drawing.Color.White
                            End If
                            newRec(j) = temp.ToString("ddd")
                            newRec(j + 1) = temp.ToString("hh tt")
                            If (newRec(j + 1) = "12 PM") Then
                                newRec(j + 1) = "noon"
                            End If
                            If (newRec(j + 1) = "12 AM") Then
                                newRec(j + 1) = "midnight"
                            End If

                        End If

                    Next j
                    newrow = .Add(newRec)
                    dgvTZMatrix.Rows(newrow).Height = dgvTZMatrix.Rows(newrow).Height - 4
                    For j = 0 To dgvTZMatrix.Columns.Count - 1
                        dgvTZMatrix.Rows(newrow).Cells(j).Style.BackColor = newRecHighlight(j)
                        dgvTZMatrix.Rows(newrow).Cells(j).Style.ForeColor = My.Settings.workHoursFontColor

                        If (dgvTZMatrix.Rows(newrow).Cells(j).Value = "noon") Then
                            dgvTZMatrix.Rows(newrow).Cells(j).Style.ForeColor = My.Settings.noonFontColor
                            dgvTZMatrix.Rows(newrow).Cells(j).Style.BackColor = My.Settings.noonBackgroundColor

                        End If
                        If dgvTZMatrix.Rows(newrow).Cells(j).Value = "midnight" Then
                            dgvTZMatrix.Rows(newrow).Cells(j).Style.ForeColor = My.Settings.noonFontColor
                            dgvTZMatrix.Rows(newrow).Cells(j).Style.BackColor = My.Settings.noonBackgroundColor
                        End If
                    Next
                    iterateTime = DateAdd(DateInterval.Hour, 1, iterateTime)
                End With
            Next
            dgvTZMatrix.ClearSelection()
        Catch ex As Exception
            InternationalTimes.ShowError(ex.Message, ex.StackTrace)
        End Try
    End Sub
    Sub AddAppointmentTimeZoneColumns()
        Dim i As Integer
        Dim formRegions As WindowFormRegionCollection = Globals.FormRegions(Globals.ThisAddIn.Application.ActiveInspector())
        Dim colIndex As Integer


        Me.dgvTZMatrix.Columns.Clear()
        ' add organizers timezone 
        colIndex = Me.dgvTZMatrix.Columns.Add(cApp.StartTimeZone.ID, cApp.StartTimeZone.Name)
        dgvTZMatrix.Columns(colIndex).Tag = 2
        colIndex = Me.dgvTZMatrix.Columns.Add("", "")
        dgvTZMatrix.Columns(colIndex).Tag = 1

        ' add timezones that are in the time zone grid on the appointment on page 1
        With formRegions.InternationalTimes.dgvTimes
            'startDate.CalendarTitleBackColor = formRegions.InternationalTimes._ThemeHeaderCellBackgroundColor
            'endDate.CalendarTitleBackColor = formRegions.InternationalTimes._ThemeHeaderCellBackgroundColor
            'startTime.CalendarTitleBackColor = formRegions.InternationalTimes._ThemeHeaderCellBackgroundColor
            'endTime.CalendarTitleBackColor = formRegions.InternationalTimes._ThemeHeaderCellBackgroundColor
            'startDate.CalendarTitleForeColor = Color.Black
            'endDate.CalendarTitleForeColor = Color.Black
            'startTime.CalendarTitleForeColor = Color.Black
            'endTime.CalendarTitleForeColor = Color.Black

            dgvTZMatrix.ColumnHeadersDefaultCellStyle.BackColor = formRegions.InternationalTimes._ThemeHeaderCellBackgroundColor
            dgvTZMatrix.BackgroundColor = formRegions.InternationalTimes._ThemePageBackgroudColor
            Dim rtv(.Rows.Count) As String
            Try
                For i = 0 To .Rows.Count - 1

                    colIndex = Me.dgvTZMatrix.Columns.Add(.Rows(i).Cells("dgvTimeZone").Value, .Rows(i).Cells("dgvLocation").Value)
                    dgvTZMatrix.Columns(colIndex).Tag = 2
                    colIndex = Me.dgvTZMatrix.Columns.Add("", "")
                    dgvTZMatrix.Columns(colIndex).Tag = 1
                Next
            Catch ex As Exception
                InternationalTimes.ShowError(ex.Message, ex.StackTrace)

            End Try
            PrepareSpanValue()
            '' populate the drop down control above the grid
            'timeZones.DataSource = formRegions.InternationalTimes.dtAvailableTimeZones
            'timeZones.DisplayMember = "DisplayName"
            'timeZones.ValueMember = "ValueName"
        End With
    End Sub


    'Private Sub dgvTZMatrix_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvTZMatrix.CellPainting
    '    If (e.RowIndex = -1) And (e.ColumnIndex > -1) Then

    '        Using gridBrush As System.Drawing.Brush = New System.Drawing.SolidBrush(dgvTZMatrix.GridColor), backColorBrush As System.Drawing.Brush = New System.Drawing.SolidBrush(e.CellStyle.BackColor)

    '            Using gridLinePen As System.Drawing.Pen = New System.Drawing.Pen(gridBrush)
    '                ' Clear cell   
    '                e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

    '                ' Draw line (bottom border and right border of current cell)   
    '                'If next row cell has different content, only draw bottom border line of current cell   
    '                'If e.RowIndex < dgvTZMatrix.Rows.Count - 2 AndAlso dgvTZMatrix.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then
    '                '    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
    '                'End If

    '                ' Draw right border line of current cell   
    '                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)

    '                ' draw/fill content in current cell, and fill only one cell of multiple same cells   
    '                If Not e.Value Is Nothing Then
    '                    If e.RowIndex > 0 AndAlso dgvTZMatrix.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then
    '                    Else
    '                        e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, System.Drawing.Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, System.Drawing.StringFormat.GenericDefault)
    '                    End If
    '                End If

    '                e.Handled = True
    '            End Using
    '        End Using


    '    End If

    'End Sub

    Private Sub TimeZonesPage_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        ShowTZMatrix()

    End Sub

    Private Sub dgvTZMatrix_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTZMatrix.CellMouseEnter

        dgvTZMatrix.ClearSelection()
        If e.RowIndex >= 0 Then
            dgvTZMatrix.Rows(e.RowIndex).Selected = True
        End If
    End Sub
    Sub PrepareSpanValue()
        Dim col As System.Windows.Forms.DataGridViewColumn
        For Each col In dgvTZMatrix.Columns
            If col.Tag > 1 Then
                For i = 1 To col.Tag - 1
                    dgvTZMatrix.Columns(col.Index + i).tag = -1

                Next
            End If
        Next

    End Sub


    Private Sub dgvTZMatrix_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvTZMatrix.CellPainting

        If e.RowIndex = -1 Then
            Dim j As Integer
            'todo: fix runtime potential issue here
            Select Case dgvTZMatrix.Columns(e.ColumnIndex).Tag
                Case Is > 1
                    dgvTZMatrix.ColumnHeadersBorderStyle = Windows.Forms.DataGridViewHeaderBorderStyle.Raised


                    Dim w2 As Integer = 0
                    Dim r1 As System.Drawing.Rectangle = dgvTZMatrix.GetCellDisplayRectangle(e.ColumnIndex, -1, True)

                    For j = 2 To dgvTZMatrix.Columns(e.ColumnIndex).Tag
                        w2 = w2 + dgvTZMatrix.GetCellDisplayRectangle(e.ColumnIndex + (j - 1), -1, True).Width
                    Next
                    'w2 = dgvTZMatrix.GetCellDisplayRectangle(e.ColumnIndex + 1, -1, True).Width

                    'r1.Height -= 1
                    r1.X -= 1
                    r1.Width = r1.Width + w2 + 1 '+ w2 '- 2
                    e.Graphics.FillRectangle(New System.Drawing.SolidBrush(dgvTZMatrix.ColumnHeadersDefaultCellStyle.BackColor), _
                                             r1)
                    'e.Graphics.DrawRectangle(Drawing.Pens.Black, r1)
                    DrawBorder(e.Graphics, r1, Windows.Forms.BorderStyle.Fixed3D, False)

                    Dim format As System.Drawing.StringFormat = New System.Drawing.StringFormat()
                    format.Alignment = System.Drawing.StringAlignment.Center
                    format.LineAlignment = System.Drawing.StringAlignment.Center


                    e.Graphics.DrawString(dgvTZMatrix.Columns(e.ColumnIndex).HeaderText, _
                            dgvTZMatrix.ColumnHeadersDefaultCellStyle.Font, _
                            New System.Drawing.SolidBrush(dgvTZMatrix.ColumnHeadersDefaultCellStyle.ForeColor), _
                            r1, format)
                    e.Handled = True
                Case Is < 0
                    e.Handled = True
            End Select

        End If

    End Sub
    Private Sub DrawBorder(ByVal gr As Graphics, ByVal rect As  _
    Rectangle, ByVal border_style As System.Windows.Forms.BorderStyle, Optional _
    ByVal sunken As Boolean = True)
        Select Case border_style
            Case BorderStyle.FixedSingle
                rect.Width -= 1
                rect.Height -= 1
                gr.DrawRectangle(Pens.Black, rect)
            Case BorderStyle.Fixed3D
                Dim colors() As Color
                If sunken Then
                    colors = New Color() { _
                        SystemColors.ControlDark, _
                        SystemColors.ControlDarkDark, _
                        SystemColors.ControlLightLight, _
                        SystemColors.ControlLight _
                    }
                Else
                    colors = New Color() { _
                        SystemColors.ControlLightLight, _
                        SystemColors.ControlLight, _
                        SystemColors.ControlDark, _
                        SystemColors.ControlDarkDark _
                    }
                End If
                Dim p As Pen
                p = New Pen(colors(0))
                gr.DrawLine(p, rect.X, rect.Y + rect.Height - _
                    1, rect.X, rect.Y)
                gr.DrawLine(p, rect.X, rect.Y, rect.X + _
                    rect.Width - 1, rect.Y)
                p = New Pen(colors(1))
                gr.DrawLine(p, rect.X + 1, rect.Y + rect.Height _
                    - 2, rect.X + 1, rect.Y + 1)
                gr.DrawLine(p, rect.X + 1, rect.Y + 1, rect.X + _
                    rect.Width - 2, rect.Y + 1)
                p = New Pen(colors(2))
                gr.DrawLine(p, rect.X, rect.Y + rect.Height - _
                    1, rect.X + rect.Width - 1, rect.Y + _
                    rect.Height - 1)
                gr.DrawLine(p, rect.X + rect.Width - 1, rect.Y _
                    + rect.Height - 1, rect.X + rect.Width - 1, _
                    rect.Y)
                p = New Pen(colors(3))
                gr.DrawLine(p, rect.X + 1, rect.Y + rect.Height _
                    - 2, rect.X + rect.Width - 2, rect.Y + _
                    rect.Height - 2)
                gr.DrawLine(p, rect.X + rect.Width - 2, rect.Y _
                    + rect.Height - 2, rect.X + rect.Width - 2, _
                    rect.Y + 1)
        End Select
    End Sub

    'Private Sub startDate_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '    cApp.Start = startDate.Value
    '    startTime.Value = cApp.Start
    'End Sub

    'Private Sub startTime_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '    cApp.Start = startTime.Value
    '    startDate.Value = cApp.Start
    'End Sub

    'Private Sub endDate_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '    cApp.End = endDate.Value
    '    endTime.Value = cApp.End
    'End Sub

    'Private Sub endTime_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '    cApp.End = endTime.Value
    '    endDate.Value = cApp.End
    'End Sub
End Class
